<template>
	<!-- <view>您输入的现状和困惑：{{kunHuo }}</view> -->
	<view class="text" v-html="markdownToHtml(text)">
	</view>
</template>

<script>
	import {
		getResultByType
	} from '../../api/index.js'
	export default {
		data() {
			return {
				timer1: null,
				type: '', // 类型 婚恋 事业 健康 困惑
				text: '', // 返回给用户看的内容
				oid: '', // orderId 
				kunHuo: '', // 困惑内容
			}
		},
		onLoad(options) {
			this.type = options.type
			this.oid = options.oid
		},
		onShow() {
			this.getTextByType(this.type)
		},
		onHide() {
			if (this.timer1) {
				clearTimeout(this.timer1)
			}
		},
		onUnload() {
			if (this.timer1) {
				clearTimeout(this.timer1)
			}
		},
		methods: {
			getTextByType(type) {
				if (type === 'hunlian') {
					this.getTextByIndex(0)
				} else if (type === 'shiye') {
					this.getTextByIndex(1)
				} else if (type === 'jiankang') {
					this.getTextByIndex(2)
				} else if (type === 'kunhuo') {
					this.getTextByIndex(3)
				}
			},
			// 获取婚恋类型的建议
			async getTextByIndex(index) {
				uni.showLoading({
					title: '加载中'
				})
				const res = await getResultByType({
					oid: this.oid,
					index
				})
				if (res.status === 200) {
					const str = res.data.content
					this.text = str
					this.kunHuo = res.data.kunHuo
				} else if (res.status === 800) {
					uni.showToast({
						title: res.info
					})
				}
				uni.hideLoading()

			},
			markdownToHtml(markdownContent) {
				if (this.type === 'kunhuo') {
					markdownContent = `您输入的现状和困惑是：${this.kunHuo}` + '\n\n' +  markdownContent
				}
				// markdownContent = `您输入的现状和困惑是：${this.kunHuo}` + '\n\n' +  markdownContent
				// 处理标题
				markdownContent = markdownContent.replace(/^#\s(.*)$/gm, '<h2>$1</h2>');
				markdownContent = markdownContent.replace(/^##\s(.*)$/gm, '<h3>$1</h3>');
				markdownContent = markdownContent.replace(/^###\s(.*)$/gm, '<h4>$1</h4>');
				markdownContent = markdownContent.replace(/^####\s(.*)$/gm, '<h5>$1</h5>');
				markdownContent = markdownContent.replace(/^#####\s(.*)$/gm, '<h6>$1</h6>');
				markdownContent = markdownContent.replace(/^######\s(.*)$/gm, '<h7>$1</h7>');

				// 处理加粗、斜体、删除线
				markdownContent = markdownContent.replace(/\*\*(.*)\*\*/gm, '<strong>$1</strong>');
				markdownContent = markdownContent.replace(/__(.*)__/gm, '<strong>$1</strong>');
				markdownContent = markdownContent.replace(/\*(.*)\*/gm, '<em>$1</em>');
				markdownContent = markdownContent.replace(/_(.*)_/gm, '<em>$1</em>');
				markdownContent = markdownContent.replace(/~~(.*)~~/gm, '<del>$1</del>');

				// 处理链接和图片
				markdownContent = markdownContent.replace(/\[(.*?)\]\((.*?)\)/gm,
					'<a href="$2" rel="external nofollow" >$1</a>');
				markdownContent = markdownContent.replace(/!\[(.*?)\]\((.*?)\)/gm, '<img src="$2" alt="$1">');

				// 处理行内代码和代码块
				markdownContent = markdownContent.replace(/`(.*?)`/gm, '<code>$1</code>');
				markdownContent = markdownContent.replace(/```([\s\S]*?)```/gm, '<pre>$1</pre>');

				markdownContent = markdownContent.replace(/\\r\\n/g, '<br/>')

				markdownContent = markdownContent.replace(/\-\-\-/gm, '');
				markdownContent = markdownContent.replace('[您的名字]', '')
				markdownContent = markdownContent.replace('[您的身份]', '')
				markdownContent = markdownContent.replace('[您的职业]', '')
				markdownContent = markdownContent.replace('[日期]', '')
				return markdownContent
			}
		}
	}
</script>

<style>
	page {
		background-color: #201e3e;
	}
</style>

<style>
	.text {
		padding: 20rpx 20rpx 20rpx 30rpx;
		font-size: 18px;
		color: #c6c4db;
		white-space: pre-wrap;
		line-height: 1.5;
	}
</style>